#include <cstdio>
#include <queue>
const int N = 2000010;
int n, m;
int a[N];
struct List {
	int l, r, val;
} li[N];
struct Node {
	int val, id;
	bool operator<(const Node &rph) const { return val < rph.val; }
};

bool vis[N];
std::priority_queue< Node > pq;
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("flower.in", "r", stdin);
	freopen("flower.out", "w", stdout);
#endif
#endif

	scanf("%d%d", &n, &m);
	if (n / 2 < m) {
		printf("Error!");
		return 0;
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		li[i].l = i - 1;
		li[i].r = i + 1;
		li[i].val = a[i];
		pq.push({a[i], i});
	}
	li[1].l = n;
	li[n].r = 1;
	int ans = 0;
	while (m--) {
		while (vis[pq.top().id]) pq.pop();
		int id = pq.top().id;
		pq.pop();
		ans += li[id].val;
		pq.push({li[id].val = li[li[id].l].val + li[li[id].r].val - li[id].val, id});
		vis[li[id].l] = vis[li[id].r] = true;
		li[li[li[id].l].l].r = id;
		li[li[li[id].r].r].l = id;
		li[id].l = li[li[id].l].l;
		li[id].r = li[li[id].r].r;
	}
	printf("%d", ans);
	return 0;
}